<template>
  <el-dialog :title="form.customerId ? $t('common.editBtn') : $t('common.addBtn')" width="600" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="90px" v-loading="loading">
      <el-form-item :label="t('customer.customerName')" prop="customerName">
        <el-input maxlength="50" show-word-limit v-model="form.customerName" :placeholder="t('customer.inputCustomerNameTip')"/>
      </el-form-item>
      <el-form-item :label="t('customer.contact')" prop="contact">
        <el-input maxlength="50" show-word-limit v-model="form.contact" :placeholder="t('customer.inputContactTip')"/>
      </el-form-item>
      <el-form-item :label="t('customer.customerAddress')" prop="customerAddress">
        <el-input maxlength="255" show-word-limit v-model="form.customerAddress" :placeholder="t('customer.inputCustomerAddressTip')"/>
      </el-form-item>
      <el-form-item :label="t('customer.customerPhone')" prop="customerPhone">
        <el-input maxlength="11" show-word-limit v-model="form.customerPhone" :placeholder="t('customer.inputCustomerPhoneTip')"/>
      </el-form-item>
      <el-form-item :label="t('customer.remark')" prop="remark">
        <el-input type="textarea" maxlength="255" show-word-limit rows="3" v-model="form.remark" :placeholder="t('customer.inputremarkTip')"/>
      </el-form-item>
    </el-form>
    <template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
			</span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="CustomerDialog">
import {useMessage} from '/@/hooks/message';
import {getObj, addObj, putObj, validateCustomName} from '/@/api/bcp/customer';
import { useUserInfo } from '/@/stores/userInfo';
import {useI18n} from 'vue-i18n';
import {rule} from '/@/utils/validate';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const {t} = useI18n();

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

// 提交表单数据
const form = reactive({
  customerId: '',
  customerName: '',
  contact: '',
  customerAddress: '',
  customerPhone: '',
  remark: '',
  delFlag: '',
  createTime: '',
  createBy: '',
  updateTime: '',
  updateBy: '',
  protectCustomer: '0'
});

// 定义校验规则
const dataRules = ref({
  customerName: [
    {validator: rule.overLength50, trigger: 'blur'},
    {required: true, message: '客户名称不能为空', trigger: 'blur'},
    {
      validator: (rule: any, value: any, callback: any) => {
        validateCustomName(rule, value, callback, form.customerId !== '');
      },
      trigger: 'blur',
    },
  ],
  contact: [{validator: rule.overLength50, trigger: 'blur'}],
  customerPhone: [{validator: rule.overLength50, trigger: 'blur'}],
  customerAddress: [{validator: rule.overLength, trigger: 'blur'}],
  remark: [{validator: rule.overLength, trigger: 'blur'}],
});

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true;
  form.customerId = '';

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取客户信息
  if (id) {
    form.customerId = id;
    getCustomerData(id);
  }
  //联系人赋值为当前用户
  const data = useUserInfo().userInfos;
  form.contact=data.user.name;

};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    form.customerId ? await putObj(form) : await addObj(form);
    useMessage().success(t(form.customerId ? 'common.editSuccessText' : 'common.addSuccessText'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// 初始化表格数据
const getCustomerData = (id: string) => {
  // 获取客户数据
  getObj(id).then((res: any) => {
    Object.assign(form, res.data);
  });
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>
